﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace LNUGame.View
{
    /// <summary>
    /// 
    /// </summary>
    class Camera
    {
        private Vector2 m_modelCenterPosition = Vector2.Zero;

        // The scale we want to use for the view
        private float m_scale = 64.0f;

        /// <summary>
        /// Getter for the scale used
        /// </summary>
        public float Scale
        {
            get
            {
                return m_scale;
            }
        }


        /// <summary>
        /// Calculates and returns the position in the view for a given position in the model
        /// </summary>
        /// <param name="x">X-position of the model</param>
        /// <param name="y">Y-position of the model</param>
        /// <param name="viewPortSize">The size of the drawable window</param>
        /// <returns>a Vector2 detailing the position in the view</returns>
        internal Vector2 GetViewPosition(float x, float y, Vector2 viewPortSize)
        {
            Vector2 modelPosition = new Vector2(x, y);

            Vector2 modelViewPortSize = new Vector2(viewPortSize.X / m_scale, viewPortSize.Y / m_scale);

            Vector2 modelTopLeftPosition = m_modelCenterPosition - (modelViewPortSize / 2.0f);

            return (modelPosition - modelTopLeftPosition) * m_scale;
        }


        /// <summary>
        /// Centers the camera on a specified point in the view when given a point in the model
        /// </summary>
        /// <param name="newCenterPosition">The point in the model we want to center the view on</param>
        /// <param name="viewPortSize">The size of the viewport</param>
        /// <param name="levelSize">The size of the level</param>
        internal void CenterOn(Vector2 newCenterPosition, Vector2 viewPortSize, Vector2 levelSize)
        {
            m_modelCenterPosition = newCenterPosition;

            Vector2 modelViewPortSize = new Vector2(viewPortSize.X / m_scale, viewPortSize.Y / m_scale);

            // Check if the camera is outside the left side of the level.
            // If so, stop it from going further
            if (m_modelCenterPosition.X < modelViewPortSize.X / 2.0f)
            {
                m_modelCenterPosition.X = modelViewPortSize.X / 2.0f;
            }

            // Check if the camera is outside the right side of the level.
            // If so, stop it from going further
            if (m_modelCenterPosition.X > levelSize.X - modelViewPortSize.X / 2.0f)
            {
                m_modelCenterPosition.X = levelSize.X - modelViewPortSize.X / 2.0f;
            }

            // Check if the top of the camera is outside the bottom of the level.
            // If so, stop it from going further
            if (m_modelCenterPosition.Y > levelSize.Y - modelViewPortSize.Y / 2.0f)
            {
                m_modelCenterPosition.Y = levelSize.Y - modelViewPortSize.Y / 2.0f;
            }

            // Check if the top of the camera is outside the top of the level.
            // If so, stop it from going further
            if (m_modelCenterPosition.Y < modelViewPortSize.Y / 2.0f)
            {
                m_modelCenterPosition.Y = modelViewPortSize.Y / 2.0f;
            }
        }


        /// <summary>
        /// Returns the viewcoordinates of the bottom left corner of the drawable area
        /// </summary>
        /// <param name="a_viewPortSize">The size if the drawable window</param>
        /// <returns>Returns the viewcoordinates of the bottom left corner of the drawable area</returns>
        internal Vector2 GetViewPortBottomLeftPosition(Vector2 a_viewPortSize)
        {
            Vector2 viewPortBottomLeftPosition = new Vector2();
            viewPortBottomLeftPosition.X = m_modelCenterPosition.X - a_viewPortSize.X / m_scale / 2;
            viewPortBottomLeftPosition.Y = m_modelCenterPosition.Y - a_viewPortSize.Y / m_scale / 2;

            return viewPortBottomLeftPosition;
        }
    }
}
